var express = require('express');
const fs = require('fs');
var UploadRouter = express.Router();
const UploadController = require('../../controllers/upload/UploadController');

// 上传图片
const multer = require('multer');
var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    // 判断文件夹是否存在，不存在创建
    const dir = 'public/uploads/';
    if (!fs.existsSync(dir)) {
      fs.mkdirSync(dir);
    }
    cb(null, dir);
  },
  filename: function (req, file, cb) {
    cb(null, Date.now() + '.' + file.originalname.split('.')[1]);
  },
});
// const upload = multer({ dest: 'public/uploads/' });
const upload = multer({ storage: storage });

UploadRouter.post('/adminApi/upload', upload.single('file'), UploadController.upload);
UploadRouter.post('/adminApi/qiniu', upload.single('file'), UploadController.uploadQiniu);
UploadRouter.post('/adminApi/minio', upload.single('file'), UploadController.uploadMinio);

module.exports = UploadRouter;
